/**
 * 
 */
package com.hm.base.auto.helper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.hm.base.auto.RedisLock;
import com.hm.base.service.AuthorizationService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 下午2:12:58 2017年5月26日
 * @version 1.0
 * @describe
 */
@Slf4j
@Component
public class HmApplicationListenerStart implements ApplicationListener<ContextRefreshedEvent> {

	@Autowired
	private AuthorizationService authorizationService;

	@Autowired
	private RedisTemplate<String,String> redisTemplate;

	private static String ADVERTISING_LOCK = "AdEmployersPlayRuleService1234";

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		log.info("springboot 初始化完成...");
		authorizationService.refreshSystemRoleDataAuth();

	}

	public void test(int i) {
		@SuppressWarnings("resource")
		RedisLock lock = new RedisLock(redisTemplate, ADVERTISING_LOCK+System.currentTimeMillis());
		try {
			if (lock.lock()) {
				System.out.println(Thread.currentThread().getName() + "我在处理数据" + i);
				Thread.sleep(50);
				System.out.println(Thread.currentThread().getName() + "我处理数据完成" + i);
			}
			System.out.println("-----华丽分割线（" + i + "）-----------");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
		System.out.println("---------执行完第" + i + "次轮询----");
	}
}
